from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from uidpapp.forms import UIDPForm, ImageExampleForm, ImplementationExampleForm, SolutionForm, RelationshipForm
from categoriesapp.models import Category
from collectionsapp.models import Collection
from uidpapp.models import UIDP
import datetime

def show_uidp(request, uidp_id):
    current_time = datetime.datetime.now()
    return render_to_response('uidp.html', locals())

def create_uidp(request):
    if request.method == 'POST':
        form = UIDPForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            collection = form.save(cd)

            return HttpResponseRedirect('/collections/%d' % collection.id)
    else:
        form = UIDPForm()
    
    categories = Category.objects.all()
    return render_to_response('new_uidp.html', locals())

def create_uidp_example(request, collection_id, uidp_id):
    if request.method == 'POST':
        if (request.POST["type"] == "image"):
            form = ImageExampleForm(request.POST)
        else:
            form = ImplementationExampleForm(request.POST)
            
        if form.is_valid():
            cd = form.cleaned_data
            example = form.save(cd, uidp_id, collection_id)
            return HttpResponseRedirect('/collections/%d/uidps/%d' % (int(collection_id), int(uidp_id)))
        
    image_form = ImageExampleForm()
    implementation_form = ImplementationExampleForm()
    uidp = UIDP.objects.get(pk=uidp_id)
    collections = Collection.objects.all()
    collection = Collection.objects.get(pk=collection_id)
    collection_id = collection.id
    
    return render_to_response('examples_form.html', locals())

def create_uidp_solution(request, collection_id, uidp_id):
    if request.method == 'POST':
        form = SolutionForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            solution = form.save(cd, uidp_id, collection_id)

            return HttpResponseRedirect('/collections/%d' % int(collection_id))
        
    form = SolutionForm()
    uidp = UIDP.objects.get(pk=uidp_id)
    collections = Collection.objects.all()
    collection = Collection.objects.get(pk=collection_id)
    collection_id = collection.id
    
    return render_to_response('solutions_form.html', locals())

def create_uidps_rel(request, collection_id, uidp_id):
    if request.method == 'POST':
        form = RelationshipForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            print request.POST.keys()
            rel = form.save(cd, uidp_id, request.POST["uidp_target"], collection_id)

            return HttpResponseRedirect('/collections/%d' % int(collection_id))
        
    form = RelationshipForm()
    collection = Collection.objects.get(pk=collection_id)
    collection_id = collection.id
    uidps = UIDP.objects.filter(collections=collection)
    
    uidp = UIDP.objects.get(pk=uidp_id)
    collections = Collection.objects.all()
    
    return render_to_response('relationships_form.html', locals())